package practice;

import java.util.*;

public class Day36 {

    //进阶二分查找
    public int[] searchRange(int[] nums, int target) {
        int[] ret = {-1,-1};
        int left = 0;
        int n = nums.length;
        if(n == 0) {
            return ret;
        }
        int right = n-1;
        while(left < right) {
            int mid = left + (right-left)/2;
            if(nums[mid] >= target) {
                right = mid;
            }else {
                left = mid + 1;
            }
        }
        ret[0] = nums[left] == target ? left : -1;

        left = 0;
        right = n-1;
        while(left < right) {
            int mid = left + (right-left+1)/2;
            if(nums[mid] <= target) {
                left = mid;
            }else {
                right = mid-1;
            }
        }
        ret[1] = nums[left] == target ? left : -1;
        return ret;
    }

    //归并排序（链表）
    public ListNode sortList(ListNode head) {
        if(head == null || head.next == null) {
            return head;
        }
        ListNode slow = head;
        ListNode fast = head.next;
        while(fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }
        ListNode cur = slow.next;
        slow.next = null;

        ListNode left = sortList(head);
        ListNode right = sortList(cur);
        //合并
        ListNode newHead = new ListNode(0);
        ListNode newCur = newHead;
        while(left != null && right != null) {
            if(left.val < right.val) {
                newCur.next = left;
                newCur = newCur.next;
                left = left.next;
            }else {
                newCur.next = right;
                newCur = newCur.next;
                right = right.next;
            }
        }
        if(right != null) {
            newCur.next = right;
        }else {
            newCur.next = left;
        }
        return newHead.next;
    }

    public int firstMissingPositive(int[] nums) {
        int n = nums.length;
        for(int i = 0; i < n; i++) {
            while(nums[i] > 0 && nums[i]-1 < n && nums[nums[i]-1] != nums[i]) {
                swap(nums,i,nums[i]-1);
            }
        }
        for(int i = 0; i < n; i++) {
            if(nums[i]-1 != i) {
                System.out.println(Arrays.toString(nums));
                return i + 1;
            }
        }
        return n + 1;
    }
    private void swap(int[] nums, int a, int b) {
        int tmp = nums[a];
        nums[a] = nums[b];
        nums[b] = tmp;
    }

    //k个一组翻转链表
    public ListNode reverseKGroup(ListNode head, int k) {
        ListNode newHead = new ListNode(0);
        newHead.next = head;
        ListNode prev = newHead;
        ListNode cur = newHead;
        while(cur != null) {
            for(int i = 0; i < k && cur != null; i++) {
                cur = cur.next;
            }
            if(cur == null) {
                //说明不用翻转了
                break;
            }
            ListNode curNext = cur.next;
            ListNode start = prev.next;
            cur.next = null;
            prev.next = reverse(start);
            start.next = curNext;
            prev = start;
            cur = start;
        }
        return newHead.next;
    }

    private ListNode reverse(ListNode root) {
        ListNode newHead = new ListNode(0);
        ListNode cur = null;
        while(root != null) {
            ListNode curNext = root.next;
            newHead.next = root;
            root.next = cur;
            cur = root;
            root = curNext;
        }
        return newHead.next;
    }
    //合并k个升序链表
    public ListNode mergeKLists(ListNode[] lists) {
        PriorityQueue<ListNode> queue = new PriorityQueue<>((o1, o2)->{
            return o1.val-o2.val;
        });
        for(ListNode node : lists) {
            if(node != null) {
                queue.offer(node);
            }
        }

        ListNode prev = new ListNode(0);
        ListNode cur = prev;
        while(!queue.isEmpty()) {
            ListNode top = queue.poll();
            cur.next = top;
            cur = cur.next;
            if(top.next != null) {
                queue.offer(top.next);
            }
        }
        return prev.next;
    }


    //决策树的书写（熟悉）
    private static boolean[] use = new boolean[1001];
    private static int num;//用来计算总和
    private static int n;
    private static int ret = Integer.MAX_VALUE;

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int len = in.nextInt();
        n = len;
        int[] arr = new int[n];
        for(int i = 0; i < n; i++) {
            arr[i] = in.nextInt();
        }
        dfs(0,arr);
        System.out.println(ret == Integer.MAX_VALUE ? -1 : ret);
    }

    private static boolean isTrim(int a) {
        for(int i = 2; i <= Math.sqrt(a); i++) {
            if(a % i == 0) {
                return false;
            }
        }
        return true;
    }
    private static void dfs(int pos,int[] arr) {
        if(pos == n) {
            ret = Math.min(ret,num);
            return;
        }

        for(int i = 2; i <= arr[pos]; i++) {
            if(!use[i] && isTrim(i) && arr[pos] % i == 0) {
                num += i;
                use[i] = true;
                dfs(pos+1,arr);
                num -= i;
                use[i] = false;
            }
        }
    }

    //最长递增子序列
    public int lengthOfLIS(int[] nums) {
        int n = nums.length;
        int[] dp = new int[n+1];
        int pos = 0;
        for(int i = 0; i < n; i++) {
            if(pos == 0 || nums[i] > dp[pos]) {
                dp[++pos] = nums[i];
            }else {
                //使用二分查找，查找插入位置
                int left = 1;
                int right = pos;
                while(left < right) {
                    int mid = left + (right - left) / 2;
                    if(dp[mid] < nums[i]) {
                        left = mid+1;
                    }else {
                        right = mid;
                    }
                }
                dp[right] = nums[i];
            }
        }
        return pos;
    }
    //划分字母区间
    public List<Integer> partitionLabels(String s) {
        List<Integer> ret = new LinkedList<>();
        int[] dp = new int[26];
        for(int i = 0; i < s.length(); i++) {
            dp[s.charAt(i)-'a'] = i;
        }
        int start = 0;
        int end = 0;
        for(int i = 0; i < s.length(); i++) {
            end = Math.max(end,dp[s.charAt(i)-'a']);
            if(end == i) {
                ret.add(i-start+1);
                start = i+1;
            }
        }
        return ret;

    }
    public int trap(int[] height) {
        int n = height.length;
        int[] left = new int[n+1];
        int[] right = new int[n+1];
        right[n] = height[n-1];
        for(int i = 1; i <= n; i++) {
            left[i] = Math.max(left[i-1],height[i-1]);
        }
        for(int i = n-1; i > 0; i--) {
            right[i] = Math.max(right[i+1],height[i-1]);
        }
        int ret = 0;
        for(int i = 0; i < n; i++) {
            ret += (Math.min(right[i+1],left[i+1])-height[i]);
        }
        return ret;
    }

    class Node{
        Node[] arr = new Node[26];
        boolean flag;
    }

    Node root = new Node();

    public void insert(String word) {
        Node cur = root;
        for(char ch : word.toCharArray()) {
            if(cur.arr[ch-'a'] == null) {
                cur.arr[ch-'a'] = new Node();
            }
            cur = cur.arr[ch-'a'];
        }
        cur.flag = true;
    }

    public boolean search(String word) {
        Node cur = root;
        for(char ch : word.toCharArray()) {
            if(cur.arr[ch-'a'] == null) {
                return false;
            }
            cur = cur.arr[ch-'a'];
        }
        return cur.flag;
    }

    public boolean startsWith(String prefix) {
        Node cur = root;
        for(char ch : prefix.toCharArray()) {
            if(cur.arr[ch-'a'] == null) {
                return false;
            }
            cur = cur.arr[ch-'a'];
        }
        return true;
    }
}
